package web;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ServletUtilMethods {

    public static boolean isEmptyOneOfField(String... args) {
        for (String value : args) {
            if (value == null || value.isEmpty()) {
                return true;
            }
        }
        return false;
    }

    public static DateFormat getSimpleDateFormat() {
        return new SimpleDateFormat("dd.MM.yyyy");
    }

    public static Date getDateFromString(String dateInString) {
        Date date = null;
        if (dateInString.length() == 8) {
            dateInString = String.format(
                    "%s.%s.%s",
                    dateInString.substring(0, 2),
                    dateInString.substring(2, 4),
                    dateInString.substring(4));
        }

        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
        dateInString = dateInString.replace('/', '.').replace(';', '.').replace(',', '.').replace('-', '.');
        //System.out.println("dateInString=" + dateInString);
        try {
            date = sdf.parse(dateInString);
        } catch (ParseException ex) {
            System.out.println(ex.getMessage());
        }
        return date;
    }

    public static String getStringFromDate(Date date) throws ParseException {
        String str = "";
        if (date == null) {
            str = "01.01.0000";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
        return sdf.format(date);
    }

    public static void main(String[] args) throws ParseException {
        System.out.println(Locale.getDefault());
        String d = "15122001";
        Date myDate = getDateFromString(d);
        System.out.println("myDate = " + myDate);
        //System.out.println(getSimpleDateFormat().format(new Date()));
    }
}
